package tihi.com.horizotalscrolldemo;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

/**
 * Created by tihi on 2017/5/15.
 */

public class HorizontalGridView extends GridView {

    public HorizontalGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public HorizontalGridView(Context context) {
        super(context);
    }

    public HorizontalGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();

        int childWidth = ViewUtils.dp2px(88);  //ViewUtils.screenWidth() / 4;//ViewUtils.dp2px(88);
        int childHeight = ViewUtils.dp2px(80);
        int lastPadding = ViewUtils.dp2px(10);


        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(expandSpec , heightMeasureSpec);

        //设置GridView的宽度
        setMeasuredDimension(childCount * childWidth + lastPadding, childHeight);
    }

}
